
package fr.castorhoz.isi_connectivity_widget.mobiledata_widget;

import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.Log;
import fr.castorhoz.isi_connectivity_widget.ConnButton;
import fr.castorhoz.isi_connectivity_widget.R;
import fr.castorhoz.isi_connectivity_widget.database.DBAdapter;
import fr.castorhoz.isi_connectivity_widget.database.DataBaseHelper;

/**
 * @author HozakaN
 *
 */
public class MobileDataButton extends ConnButton {

	public final static String DATA_DISABLED = "dataDisabled";	
	public final static String DATA_ENABLED = "dataEnabled";

	/**
	 * debug purposes
	 */
	private static final boolean DEBUG_NETWORK_INFORMATION = true;
	private static final boolean DEBUG_WIFI = true;
	
	private static DBAdapter db;
	
	private Context context;
	
	public MobileDataButton(Context context, int widgetId) {
		this.context = context;
		this.appWidgetId = widgetId;
				
		Log.d(this.getClass().getName(), "detecting mobile data connectivity");
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mobileNetworkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (DEBUG_WIFI) {
			
		}
		if (mobileNetworkInfo != null) {
			detectMobileDataConnectivityState(mobileNetworkInfo);
		} else {
			Log.d(this.getClass().getName(), "mobileNetworkInfo is null");
		}
	}
	
	/**
	 * Calculates the state of mobileData network, and saves it into the database
	 * @param nInfo
	 */
	private void detectMobileDataConnectivityState(NetworkInfo nInfo) {
		if (DEBUG_NETWORK_INFORMATION) {
			Log.d(this.getClass().getName(), "network Extra Info : "+nInfo.getExtraInfo());
			Log.d(this.getClass().getName(), "network reason : "+nInfo.getReason());
			Log.d(this.getClass().getName(), "network Detailed State : "+nInfo.getDetailedState());	
		}
		db = new DBAdapter(context);
		if (nInfo.isConnectedOrConnecting()) {
			db.open();
			db.removeMobileDataWidgetStates();
			db.insertMobileDataWidgetState(MobileDataButton.DATA_ENABLED);
			db.close();
			Log.d(this.getClass().getName(), "mobile data network is active and this is saved into database");
		} else {
			db.open();
			db.removeMobileDataWidgetStates();
			db.insertMobileDataWidgetState(MobileDataButton.DATA_DISABLED);
			db.close();
			Log.d(this.getClass().getName(), "mobile data network is inactive and this is saved into database");
		}
	}
	
	/**
	 * Generic button. Do not use this to add a button into a widget
	 */
	public MobileDataButton() {}
	
	@Override
	public boolean getState(Context context) {		
		return getMobileDataStateFromDatabse(context);
	}
	
	
	/////////////////TODO
	@Override
	public void enableNetwork(Context context) {
	    
	    /*
	     * Getting adapted switcher for the device version
	     * switching network is not possible anymore after Froyo (api level 8, android 2.2),
	     * so we must provide two different kinds of switcher based on the device android version
	     */
		Log.d(this.getClass().getName(), "enabling mobile data");
	    MobileDataSwitcher switcher = MobileDataSwitcherFactory.getMobileDataSwitcher(Build.VERSION.SDK_INT);
	    switcher.enableNetwork(context);
	}

	@Override
	public void disableNetwork(Context context) {
	    
	    /*
	     * Getting adapted switcher for the device version
	     * switching network is not possible anymore after Froyo (api level 8, android 2.2),
	     * so we must provide two different kinds of switcher based on the device android version
	     */
		Log.d(this.getClass().getName(), "disabling mobile data");
	    MobileDataSwitcher switcher = MobileDataSwitcherFactory.getMobileDataSwitcher(Build.VERSION.SDK_INT);
	    switcher.disableNetwork(context);
	}
	
	public String getAction(){
		return "fr_imberle_connectivityWidget_CLICK_MOBILEDATA";
	}
	
	@Override
	public int getSelectorOff() {
		return R.drawable.mobiledata_selector_off;
	}
	
	@Override
	public int getSelectorOn() {
		return R.drawable.mobiledata_selector_on;
	}
	
	@Override
	public int getSelectorUnknown() {
		return R.drawable.mobiledata_selector_unknown;
	}

	/**
	 * Returns the selector that should be displayed after the mobile data network state (enabled/disabled)
	 */
	@Override
	public int getCurrentSelector() {
		return getMobileDataStateFromDatabse(context) ? getSelectorOn() : getSelectorOff();
	}

	/**
	 * Gets the state of mobile data (enabled/disabled) from database
	 * @param context
	 * @return
	 */
	public static boolean getMobileDataStateFromDatabse(Context context) {
		db = new DBAdapter(context);
        String mobileDataState = null;
        db.open();
        Cursor c = db.getMobileDataWidgetState();
        if (c.moveToNext()) {
                mobileDataState = c.getString(c.getColumnIndexOrThrow(DataBaseHelper.MOBILEDATA_STATE_COLUMNNAME));
        }
        c.close();
        db.close();
        return mobileDataState != null && mobileDataState.equals(DATA_ENABLED);
	}

}
